package com.leapp.sk.ui.fragment.message

import android.os.Bundle
import android.text.TextUtils
import android.view.View
import cc.vv.lkimcomponent.lkim.bean.LKIMMessage
import cc.vv.lkimcomponent.lkim.bean.body.LKIMImageBody
import cc.vv.lkimcomponent.lkim.bean.body.LKIMTxtBody
import cc.vv.lkimcomponent.lkim.bean.body.LKIMVideoBody
import cc.vv.lkimcomponent.lkim.bean.body.LKIMVoiceBody
import cc.vv.lkimcomponent.lkim.bean.venum.LKIMChatType
import cc.vv.lkimcomponent.lkim.bean.venum.LKIMType
import com.example.baselibrary.util.LKToastUtil
import com.example.mvp.binder.BaseDataBinder
import com.example.mvp.fragment.BaseFragmentMVP
import com.example.mvp.bean.GroupMemberEntity
import com.example.mvp.view.LKDialog
import com.leapp.sk.R
import com.leapp.sk.binder.BaseBinder
import com.example.im.SKIMSend
import com.example.util.finallist.MSG_DATA
import com.example.util.finallist.UNREAD_MESSAGE_DATAS
import com.leapp.sk.viewdelegate.im.MessageUnreadDelegate
/**
 * 未读消息
 * @author  chaohaipeng  on 2018/7/26.
 */
class UnReadMessageFragment:BaseFragmentMVP<MessageUnreadDelegate>() {
    /**
     * 消息体数据
     */
    private var mLKIMMessage:LKIMMessage?=null

    /**
     * 消息体数据
     */
    private var memberList:List<GroupMemberEntity>?=null
    /**
     * 私信
     */
    private var lkDialog: LKDialog? = null
    override fun initView(p0: Bundle?) {
        memberList = p0?.getParcelableArrayList<GroupMemberEntity>(UNREAD_MESSAGE_DATAS)
        if (memberList!=null){
            viewDelegate.initAdapter(activity,memberList!!)
        }
        mLKIMMessage=  p0?.getParcelable<LKIMMessage>(MSG_DATA)
    }

    override fun initData() {


    }
    /**
     * 私信发送
     */
    fun privateMsgDialog(){
        if (lkDialog==null){
            lkDialog = LKDialog(activity)
            lkDialog?.setMessage(R.string.str_private_message)
            lkDialog?.setLeftButton(R.string.app_cancel,View.OnClickListener {
                lkDialog?.dismiss()
            })
            lkDialog?.setRightButton(R.string.app_confirm, View.OnClickListener {
                lkDialog?.dismiss()
                for (i in memberList!!.indices ){
                    val get = memberList!!.get(i)
                    val phone = get.phone
                    if (!TextUtils.isEmpty(phone)){
                        sendMessage(mLKIMMessage!!,phone!!)
                    }

                }
                LKToastUtil.showToastShort(activity.resources.getString(R.string.app_message_send))
            })

        }
        lkDialog?.show()
    }
    override fun bindEvenListener() {
        viewDelegate.setOnClickListener(View.OnClickListener {
            if (memberList==null||mLKIMMessage==null){
                return@OnClickListener
            }
            privateMsgDialog()
        }, R.id.tv_fmur_private_message)
    }

    /**
     * 私信发送
     */
    fun sendMessage(lkmessage:LKIMMessage,conversationId:String){
        if (lkmessage==null){
            return
        }
        if (lkmessage.lkimType== LKIMType.TXT) {
            var lkImBody: LKIMTxtBody? = null
            try {
                lkImBody = lkmessage.lkIMBody as LKIMTxtBody
            } catch (e: Exception) {
                lkImBody = null
            }
            if (lkImBody!=null){
               val content=lkImBody.message
                SKIMSend.getInstance().sendTxt(conversationId!!, content, LKIMChatType.SingleChat)
            }
        } else if (lkmessage.lkimType== LKIMType.IMAGE) {
            var lkImBody: LKIMImageBody?=null
            try {
                lkImBody= lkmessage.lkIMBody as LKIMImageBody
            }catch (e:Exception){
                lkImBody
            }
            if (lkImBody!=null){
                SKIMSend.getInstance().sendImage(conversationId!!,lkImBody.localUrl,LKIMChatType.SingleChat)
            }

        }else if (lkmessage.lkimType== LKIMType.VIDEO) {
            var lkImBody: LKIMVideoBody?=null
            try {
                lkImBody = lkmessage.lkIMBody as LKIMVideoBody
            } catch (e: Exception) {
                lkImBody=null
            }
             if (lkImBody!=null){
                 val duration = lkImBody.duration
                 SKIMSend.getInstance().sendVideo(conversationId!!,lkImBody.localUrl,lkImBody.localThumbUrl,duration,LKIMChatType.SingleChat)
             }

        }else if (lkmessage.lkimType== LKIMType.VOICE) {
            var lkimVoiceBody: LKIMVoiceBody?=null
            try {
                lkimVoiceBody= lkmessage.lkIMBody as LKIMVoiceBody
            }catch (e:Exception){
                lkimVoiceBody
            }
            if (lkimVoiceBody!=null){
              val duration= lkimVoiceBody.duration
                SKIMSend.getInstance().sendVoice(conversationId!!, lkimVoiceBody.localUrl, duration, LKIMChatType.SingleChat)
            }
        }

    }


    override fun getDelegateClass(): Class<MessageUnreadDelegate> {
        return MessageUnreadDelegate::class.java
    }

    override fun getDataBinder(): BaseDataBinder<*, *> {
        return BaseBinder()
    }
}